package threadPoolTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @WorkStealingPool: 任务窃取线程池，不保证执行顺序，适合任务耗时差异较大   默认创建的并行 level 是 CPU 的核数。主线程结束，即使线程池有任务也会立即停止。
 * @author: ZBoHang
 * @time: 2021/9/6 17:49
 */
public class WorkStealingPool {

    static ExecutorService workStealingExec = Executors.newWorkStealingPool();

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        for (int i = 0; i < 10; i++) {
            final int index = i;
            workStealingExec.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " index->> " + index);
                }
            });
        }
        try {
            System.out.println("主线程休眠10s");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
